/**
 * Created by Administrator on 2017/5/12 0012.
 */


var resource;
var pixiStageW;
var pixiStageH;
var hintW;
var hintH;
var windowLength1;
var windowLength2;

var windowLength1;
var windowLength2;
var ratio=window.devicePixelRatio;
PreloadPage = function () {





    var stage = new PIXI.Container();
    var renderer = new PIXI.CanvasRenderer(256, 256, {antialias: true, transparent: true});
    renderer.view.style.border = "0px dashed black";
    renderer.resize(740, 1136);
    renderer.backgroundColor = 0x000000;
    var container = document.createElement("div");
    container.id = "LayerPreloadPage";
    // var container=document.getElementById("LayerPreloadPage");
    container.style.position = "absolute";
    document.body.appendChild(container);

    container.appendChild(renderer.view);

    document.getElementById("LayerPreloadPage").style.zIndex = 0;
    //资源加载
    var hajime = new Hajime();
    stage.addChild(hajime);
    hajime.x = window.innerWidth / 2;
    hajime.y = window.innerHeight / 2;

    var bl_loadingCompleted = false;
    var canStart = false;


    document.addEventListener('HajimeCompleted', function (e) {
        bl_loadingCompleted = true;
        resource = e.detail;

        resizeHandler();
    });




    if (window.innerWidth < window.innerHeight) {
        windowLength1 = window.innerWidth;
        windowLength2 = window.innerHeight;
    }
    else {
        windowLength1 = window.innerHeight;
        windowLength2 = window.innerWidth;
    }

    if (screenType == "hor") {
        pixiStageW = 1136;
        pixiStageH = 540;

        hintW = 740;
        hintH = 1136;
    }
    else {
        pixiStageW = 740;
        pixiStageH = 1136;

        hintW = 1136;
        hintH = 540;
    }

    resizeHandler();
    window.onresize = resizeHandler;

    var once=true;
    function resizeHandler() {
        if(once) {
            once=false;
            setTimeout(function () {
                renderer.resize(window.innerWidth*ratio, window.innerHeight*ratio);
                hajime.x=window.innerWidth/2;
                hajime.y=window.innerHeight/2;

                if (window.innerWidth < window.innerHeight) {hajime.scale.set(0.5);}
                else {hajime.scale.set(1);}


                if (bl_loadingCompleted) {
                    if (window.innerWidth < window.innerHeight) {
                        windowLength1 = window.innerWidth;
                        windowLength2 = window.innerHeight;
                        if (screenType == "hor") {
                            document.getElementById("LayerHintScreen").style.zIndex = 2;
                            document.getElementById("Layer2D").style.zIndex = 1;
                            document.dispatchEvent(new CustomEvent("ScreenChange", {detail: "hint"}));
                        }
                        else {
                            document.getElementById("LayerHintScreen").style.zIndex = 1;
                            document.getElementById("Layer2D").style.zIndex = 2;
                            if (!canStart) {
                                canStart = true;
                                document.dispatchEvent(new CustomEvent("Start"));
                            }
                            document.dispatchEvent(new CustomEvent("ScreenChange", {detail: "main"}));
                        }
                    }
                    else {
                        windowLength1 = window.innerHeight;
                        windowLength2 = window.innerWidth;
                        if (screenType == "hor") {
                            document.getElementById("LayerHintScreen").style.zIndex = 1;
                            document.getElementById("Layer2D").style.zIndex = 2;
                            if (!canStart) {
                                canStart = true;
                                document.dispatchEvent(new CustomEvent("Start"));
                            }
                            document.dispatchEvent(new CustomEvent("ScreenChange", {detail: "main"}));
                        }
                        else {
                            document.getElementById("LayerHintScreen").style.zIndex = 2;
                            document.getElementById("Layer2D").style.zIndex = 1;
                            document.dispatchEvent(new CustomEvent("ScreenChange", {detail: "hint"}));
                        }
                    }
                }
                once=true;
            }, 500)
        }
    }


    gameLoop();
    function gameLoop() {
        requestAnimationFrame(gameLoop);
        //Render The Stage
        renderer.render(stage);

    }

}